package by.grsu.shedule.database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.preference.PreferenceManager;
import by.grsu.shedule.R;
import by.grsu.shedule.api.SheduleAPI;
import by.grsu.shedule.constants.ApplicationConstants;
import by.grsu.shedule.fragment.DateFragment;
import by.grsu.shedule.model.DayFromDB;
import by.grsu.shedule.util.CalendarHelper;

public class ImportantLessonManager {

	public static final String IMPORTANT_LESSON_MANAGER = "++IMPORTANT_LESSON_MANAGER++";

	private static final String TAG = ImportantLessonManager.class
			.getSimpleName();

	private static final int DATABASE_VERSION = 1;

	private static final String DATABASE_NAME = "importantLesson.store.db";

	private DBImportantLessonHelper dbHelper;

	private SQLiteDatabase dbReader;

	private SQLiteDatabase dbWriter;

	private Context mContext;

	public ImportantLessonManager(final Context context) {
		dbHelper = new DBImportantLessonHelper(context, DATABASE_NAME, null,
				DATABASE_VERSION);
		dbReader = dbHelper.getReadableDatabase();
		dbWriter = dbHelper.getWritableDatabase();
		mContext = context;
	}

	public long addNewLesson(String subject, String teacher, String group) {
		return dbWriter.insert(DBImportantLessonHelper.IMPORTANT_LESSON_TABLE,
				null, getContentValues(subject, teacher, group));
	}

	public void deleteLesson(String subject, String teacher, String course,
			String group) {
		dbWriter.delete(DBImportantLessonHelper.IMPORTANT_LESSON_TABLE,
				DBImportantLessonHelper.COURSE_SELECTION + "? AND "
						+ DBImportantLessonHelper.GROUP_SELECTION + "? AND "
						+ DBImportantLessonHelper.SUBJECT_SELECTION + "? AND "
						+ DBImportantLessonHelper.TEACHER_SELECTION + "?",
				new String[] { course, group, subject, teacher });
	}

	private ContentValues getContentValues(String subject, String teacher,
			String group) {
		ContentValues values = new ContentValues();
		values.put(
				DBImportantLessonHelper.TABLE_COLUMNS[DBImportantLessonHelper.SUBJECT_COL_INDEX],
				subject);
		values.put(
				DBImportantLessonHelper.TABLE_COLUMNS[DBImportantLessonHelper.TEACHER_COL_INDEX],
				teacher);
		values.put(
				DBImportantLessonHelper.TABLE_COLUMNS[DBImportantLessonHelper.GROUP_COL_INDEX],
				group);
		values.put(
				DBImportantLessonHelper.TABLE_COLUMNS[DBImportantLessonHelper.COURSE_COL_INDEX],
				SheduleAPI.getCurrentCourse());
		values.put(
				DBImportantLessonHelper.TABLE_COLUMNS[DBImportantLessonHelper.LANGUAGE_COL_INDEX],
				getLanguageFromPrefs());
		return values;
	}

	private String getLanguageFromPrefs() {
		return PreferenceManager.getDefaultSharedPreferences(mContext)
				.getString(ApplicationConstants.LANGUAGE_LIST_PREFERENCE,
						mContext.getResources().getString(R.string.translit));
	}

	public boolean getLesson(String subject, String teacher, String group) {
		Cursor cursor = dbReader.query(
				DBImportantLessonHelper.IMPORTANT_LESSON_TABLE, null,
				DBImportantLessonHelper.SUBJECT_SELECTION + "? AND "
						+ DBImportantLessonHelper.TEACHER_SELECTION + "? AND "
						+ DBImportantLessonHelper.GROUP_SELECTION + "?",
				new String[] { subject, teacher, group }, null, null, null);
		return cursor.moveToFirst();
	}

	public List<DayFromDB> getAllLesson() {
		Cursor cursor = dbReader.query(
				DBImportantLessonHelper.IMPORTANT_LESSON_TABLE, null, null,
				null, null, null, null);
		List<DayFromDB> resultList = new ArrayList<DayFromDB>();
		if (cursor.moveToFirst()) {
			resultList.add(getLesson(cursor));
		}
		cursor.close();
		return resultList;
	}

	private DayFromDB getLesson(Cursor cursor) {
		return new DayFromDB(
				cursor.getString(DBImportantLessonHelper.TEACHER_COL_INDEX),
				cursor.getString(DBImportantLessonHelper.SUBJECT_COL_INDEX),
				cursor.getString(DBImportantLessonHelper.GROUP_COL_INDEX));
	}

}
